﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.Xml.Linq;

public partial class admin_Banners_Details : System.Web.UI.Page
{
  BannerBLL bannerData = new BannerBLL();
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      Session["url_back_admin"] = Request.RawUrl;

      LoadTitle();
      LoadPosition();
      if (Request.QueryString["Id"] != null)
        Filldata();
    }
  }
  private void Filldata()
  {
    int id = Convert.ToInt32(Request.QueryString["Id"]);
    Banner banner = bannerData.GetById(id);
    txtName.Text = banner.Name;
    txtLink.Text = banner.Link;
    dropPosition.SelectedValue = banner.Position.ToString();
    if (banner.isEnable == true) radioEnable.Checked = true;
    else radioVisible.Checked = true;
  }
  private void LoadPosition()
  {
    dropPosition.Items.Clear();
    XElement xElement = XElement.Load(MyConfig.serverPath + "XML/Config.xml");
    var xPosition = xElement.Descendants("Banners").Descendants("Position");
    foreach (XElement item in xPosition)
    {
      dropPosition.Items.Add(new ListItem(item.Element("Name").Value, item.Attribute("Id").Value));
    }
  }
  private void LoadTitle()
  {
    if (Request.QueryString["Id"] != null)
    {
      h3Title.InnerText = "Chỉnh sửa Banner";
    }
  }
  private bool isValidate()
  {
    if(!fileUpload.HasFile && Request.QueryString["Id"] == null)
    {
      Myfunction.ThongBao(lbThongBao, "Chọn ảnh banner");
      return false;
    }
    return true;
  }
  private void ResetForm()
  {
    txtName.Text = "";
    txtLink.Text = "";
    radioEnable.Checked = true;
  }
  private void Add()
  {
    Banner banner = new Banner();

    banner.Name = txtName.Text;
    banner.Link = txtLink.Text;
    banner.Position = Convert.ToInt32(dropPosition.SelectedValue);
    banner.isEnable = (radioEnable.Checked ? true : false);
    banner.Sort = bannerData.GetMaxSort(banner.Position);

    if (fileUpload.HasFile)
    {
      banner.Image = Myfunction.CopyFile(fileUpload.PostedFile, "Images/Banner");
    }
    bannerData.Add(banner);
    Myfunction.ThongBao(lbThongBao, "Banner đã được cập nhật");
  }
  private void Update()
  {
    int id = Convert.ToInt32(Request.QueryString["Id"]);
    Banner banner = bannerData.GetById(id);

    banner.Name = txtName.Text;
    banner.Link = txtLink.Text;
    banner.Position = Convert.ToInt32(dropPosition.SelectedValue);
    if (fileUpload.HasFile)
    {
      if (banner.Image == "" || banner.Image == null)
        banner.Image = Myfunction.CopyFile(fileUpload.PostedFile, "Images/Banner");
      else
        banner.Image = Myfunction.UpdateFile(fileUpload.PostedFile, "Images/Banner", banner.Image);
    }
    banner.isEnable = (radioEnable.Checked ? true : false);

    bannerData.Update(banner);

    Myfunction.ThongBao(lbThongBao, "Banner đã được cập nhật");
  }
  protected void btnOk_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      Myfunction.ThongBao(lbThongBao, "Bạn không có quyền thực hiện thao tác này ! ");
      return;
    }
    try
    {
      lbThongBao.Text = "";
      if (isValidate())
      {
        if (Request.QueryString["Id"] == null)
        {
          Add();
          ResetForm();
        }
        else
          Update();
      }
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao, ex.Message);
    }
  }
}